from PySide6.QtGui import QIntValidator,QDoubleValidator
from PySide6.QtWidgets import (QToolBox, QWidget, QGridLayout, QLineEdit, QLabel, QVBoxLayout
    , QRadioButton, QCheckBox)


class ChuliwayBox(QToolBox):
    def __init__(self):
        super().__init__()

        # 处理方式：图像增强
        self.zengqiangWidget = QWidget()
        self.zengqiangWidgetLayout = QGridLayout()
        self.zengqiangWidgetliangdu = QLineEdit(self)#亮度
        self.zengqiangWidgetliangdu.setValidator(QDoubleValidator(0.0, 300.0, 2))
        self.zengqiangWidgetduibidu = QLineEdit(self)
        self.zengqiangWidgetduibidu.setValidator(QDoubleValidator(0.0, 300.0, 2))
        self.zengqiangWidgetbaohedu = QLineEdit(self)#亮度
        self.zengqiangWidgetbaohedu.setValidator(QDoubleValidator(0.0, 300.0, 2))
        self.zengqiangWidgetruihua = QLineEdit(self)
        self.zengqiangWidgetruihua.setValidator(QDoubleValidator(0.0, 300.0, 2))

        self.zengqiangWidgetLayout.addWidget(QLabel('亮度：'), 0, 0)
        self.zengqiangWidgetLayout.addWidget(self.zengqiangWidgetliangdu, 0, 1)
        self.zengqiangWidgetLayout.addWidget(QLabel('%'), 0, 3)
        self.zengqiangWidgetLayout.addWidget(QLabel('对比度：'), 1, 0)
        self.zengqiangWidgetLayout.addWidget(self.zengqiangWidgetduibidu, 1, 1)
        self.zengqiangWidgetLayout.addWidget(QLabel('%'), 1, 3)

        self.zengqiangWidgetLayout.addWidget(QLabel('饱和度：'), 2, 0)
        self.zengqiangWidgetLayout.addWidget(self.zengqiangWidgetbaohedu, 2, 1)
        self.zengqiangWidgetLayout.addWidget(QLabel('%'), 2, 3)
        self.zengqiangWidgetLayout.addWidget(QLabel('锐化：'), 3, 0)
        self.zengqiangWidgetLayout.addWidget(self.zengqiangWidgetruihua, 3, 1)
        self.zengqiangWidgetLayout.addWidget(QLabel('%'), 3, 3)
        self.zengqiangWidget.setLayout(self.zengqiangWidgetLayout)
        self.addItem(self.zengqiangWidget, '图像增强')

        self.zengqiangWidgetliangdu.setText('100.00')
        self.zengqiangWidgetduibidu.setText('100.00')
        self.zengqiangWidgetbaohedu.setText('100.00')
        self.zengqiangWidgetruihua.setText('100.00')

        # 处理方式：比例
        self.biliWidget = QWidget()
        self.biliWidgetLayout = QGridLayout()
        self.biliWidgetbilikuan = QLineEdit(self)
        self.biliWidgetbiligao = QLineEdit(self)
        self.biliWidgetbilikuan.setValidator(QIntValidator(self))
        self.biliWidgetbiligao.setValidator(QIntValidator(self))
        self.biliWidgetLayout.addWidget(QLabel('宽高比：'), 0, 0)
        self.biliWidgetLayout.addWidget(self.biliWidgetbilikuan, 0, 1)
        self.biliWidgetLayout.addWidget(QLabel(':'), 0, 2)
        self.biliWidgetLayout.addWidget(self.biliWidgetbiligao, 0, 3)
        self.biliWidget.setLayout(self.biliWidgetLayout)
        self.addItem(self.biliWidget, '比例')

        self.biliWidgetbilikuan.setText('16')
        self.biliWidgetbiligao.setText('9')

        # 处理方式：尺寸
        self.chicunWidget = QWidget()
        self.chicunWidgetLayout = QGridLayout()
        self.chicunWidgetkuan = QLineEdit(self)
        self.chicunWidgetkuan.setValidator(QIntValidator(self))
        self.chicunWidgetgao = QLineEdit(self)
        self.chicunWidgetgao.setValidator(QIntValidator(self))
        self.chicunWidgetLayout.addWidget(QLabel('宽：'), 0, 0)
        self.chicunWidgetLayout.addWidget(self.chicunWidgetkuan, 0, 1)
        self.chicunWidgetLayout.addWidget(QLabel('高：'), 1, 0)
        self.chicunWidgetLayout.addWidget(self.chicunWidgetgao, 1, 1)
        self.chicunWidget.setLayout(self.chicunWidgetLayout)
        self.addItem(self.chicunWidget, '尺寸')

        self.chicunWidgetkuan.setText('1920')
        self.chicunWidgetgao.setText('1080')


        # 处理方式：裁剪与拉伸
        self.cutWidget = QWidget()
        self.cutWidgetLayout = QVBoxLayout()
        self.cutWidgetshiying = QRadioButton('适应', self.cutWidget)
        self.cutWidgetcaijian = QRadioButton('裁剪', self.cutWidget)
        self.cutWidgetlashen = QRadioButton('拉伸', self.cutWidget)

        self.cutWidgetLayout.addWidget(self.cutWidgetshiying)
        self.cutWidgetLayout.addWidget(self.cutWidgetcaijian)
        self.cutWidgetLayout.addWidget(self.cutWidgetlashen)
        self.cutWidget.setLayout(self.cutWidgetLayout)
        self.addItem(self.cutWidget, "裁剪与拉伸")

        self.cutWidgetshiying.setChecked(True)


        # 处理方式：格式
        self.geshiWidget = QWidget()
        self.geshiWidgetLayout = QVBoxLayout()
        self.geshiWidgetjpg = QRadioButton('jpeg', self.geshiWidget)
        self.geshiWidgetpng = QRadioButton('png', self.geshiWidget)
        self.geshiWidgetjingtaigif = QRadioButton('gif', self.geshiWidget)

        self.geshiWidgetLayout.addWidget(self.geshiWidgetjpg)
        self.geshiWidgetLayout.addWidget(self.geshiWidgetpng)
        self.geshiWidgetLayout.addWidget(self.geshiWidgetjingtaigif)
        self.geshiWidget.setLayout(self.geshiWidgetLayout)
        self.addItem(self.geshiWidget, "文件格式")

        self.geshiWidgetpng.setChecked(True)



        # 选择处理方式
        self.chooseWidget = QWidget()
        self.chooseWidgetLayout = QGridLayout()
        self.chooseWidgetzengqiang = QCheckBox(self.chooseWidget)
        self.chooseWidgetchicun = QCheckBox(self.chooseWidget)
        self.chooseWidgetbili = QCheckBox(self.chooseWidget)
        self.chooseWidgetgeshi = QCheckBox(self.chooseWidget)

        self.chooseWidgetLayout.addWidget(QLabel('图像增强：'),0,0)
        self.chooseWidgetLayout.addWidget(self.chooseWidgetzengqiang,0,1)
        self.chooseWidgetLayout.addWidget(QLabel('修改宽高比：'),1,0)
        self.chooseWidgetLayout.addWidget(self.chooseWidgetbili,1,1)
        self.chooseWidgetLayout.addWidget(QLabel('修改尺寸：'),2,0)
        self.chooseWidgetLayout.addWidget(self.chooseWidgetchicun,2,1)
        self.chooseWidgetLayout.addWidget(QLabel('修改格式：'),3,0)
        self.chooseWidgetLayout.addWidget(self.chooseWidgetgeshi,3,1)
        self.chooseWidget.setLayout(self.chooseWidgetLayout)
        self.insertItem(0,self.chooseWidget,'选择处理方式')
        self.setCurrentIndex(0)

        self.chooseWidgetzengqiang.checkStateChanged.connect(self.zengqiangboxcheck)
        self.chooseWidgetchicun.checkStateChanged.connect(self.chicunboxcheck)
        self.chooseWidgetbili.checkStateChanged.connect(self.biliboxcheck)
        self.chooseWidgetgeshi.checkStateChanged.connect(self.geshiboxcheck)

        self.zengqiangboxcheck()
        self.chicunboxcheck()
        self.biliboxcheck()
        self.geshiboxcheck()

        self.chicunWidgetkuan.editingFinished.connect(self.changekuan)
        self.chicunWidgetgao.editingFinished.connect(self.changegao)

    def zengqiangboxcheck(self):
        """
        点击增强框
        :return:
        """
        if self.chooseWidgetzengqiang.isChecked():
            self.zengqiangWidget.setEnabled(True)
        else:
            self.zengqiangWidget.setEnabled(False)

    def chicunboxcheck(self):
        """
        点击尺寸框
        :return:
        """
        if self.chooseWidgetchicun.isChecked():
            self.chicunWidget.setEnabled(True)
            self.cutWidget.setEnabled(True)
        else:
            self.chicunWidget.setEnabled(False)
            if not self.biliWidget.isEnabled():
                self.cutWidget.setEnabled(False)
    def biliboxcheck(self):
        """
        点击比例框
        :return:
        """
        if self.chooseWidgetbili.isChecked():
            self.cutWidget.setEnabled(True)

            if self.chicunWidgetkuan.text() != 0 and self.chicunWidgetgao.text() != 0:
                self.chicunWidgetkuan.setText('0')
            self.biliWidget.setEnabled(True)
        else:
            self.biliWidget.setEnabled(False)
            if not self.chicunWidget.isEnabled():
                self.cutWidget.setEnabled(False)
    def geshiboxcheck(self):
        """
        点击格式框
        :return:
        """
        if self.chooseWidgetgeshi.isChecked():
            self.geshiWidget.setEnabled(True)
        else:
            self.geshiWidget.setEnabled(False)

    def changekuan(self):
        if (self.chicunWidgetkuan.text() != 0 and self.chicunWidgetgao.text() != 0
                and self.chooseWidgetbili.isChecked()):
            self.chicunWidgetgao.setText('0')
    def changegao(self):
        if (self.chicunWidgetkuan.text() != 0 and self.chicunWidgetgao.text() != 0
                and self.chooseWidgetbili.isChecked()):
            self.chicunWidgetkuan.setText('0')

    def getchuliway(self):
        # 创建字典来存储数据
        data = {}

        # 获取处理方式页面的数据
        choose_data = {
            '增强': self.chooseWidgetzengqiang.isChecked(),
            '尺寸': self.chooseWidgetchicun.isChecked(),
            '比例': self.chooseWidgetbili.isChecked(),
            '格式': self.chooseWidgetgeshi.isChecked()
        }

        if choose_data['尺寸'] or choose_data['比例'] ==True:
            choose_data['裁剪与拉伸']=True
        else :
            choose_data['裁剪与拉伸']=False
        data['处理方式'] = choose_data

        if data['处理方式']['增强']:
            # 获取文件格式页面的数据
            zengqiang_data = {
                '亮度':float(self.zengqiangWidgetliangdu.text())/100.0,
                '对比度':float(self.zengqiangWidgetduibidu.text())/100.0,
                '饱和度':float(self.zengqiangWidgetbaohedu.text())/100.0,
                '锐化':float(self.zengqiangWidgetruihua.text())/100.0,
            }
            data['增强'] = zengqiang_data

        if data['处理方式']['尺寸']:
            # 获取尺寸页面的数据
            chicun_data = {'宽': int(self.chicunWidgetkuan.text()),
                           '高': int(self.chicunWidgetgao.text())}
            data['尺寸'] = chicun_data

        if data['处理方式']['比例']:
            # 获取比例页面的数据
            bili_data = {'宽': int(self.biliWidgetbilikuan.text()),
                         '高': int(self.biliWidgetbiligao.text())}
            data['比例'] = bili_data

        if data['处理方式']['裁剪与拉伸']:
            # 获取文件格式页面的数据
            cut_data = 'no'
            radio_buttons = [self.cutWidgetshiying, self.cutWidgetcaijian, self.cutWidgetlashen]
            for button in radio_buttons:
                if button.isChecked():
                    cut_data = button.text()
                    break  # 只需要保存一个被选中的单选按钮
            data['裁剪与拉伸'] = cut_data

        if data['处理方式']['格式']:
            # 获取文件格式页面的数据
            geshi_data = 'no'
            if self.geshiWidgetjpg.isChecked():
                geshi_data = 'JPEG'
            elif self.geshiWidgetpng.isChecked():
                geshi_data = 'PNG'
            elif self.geshiWidgetjingtaigif.isChecked():
                geshi_data = 'GIF'
            else:
                data['处理方式']['格式']=False
            data['格式'] = geshi_data

        return data